<?php

namespace Topxia\Service\Kuke\Dao\Impl;

use Topxia\Service\Common\BaseDao;
use Topxia\Service\Kuke\Dao\PromoDao;

class PromoDaoImpl extends BaseDao implements PromoDao
{
    protected $table = 'Promo';

	public function getPromo($conditions,$start,$limit)
	{
        $sql = "SELECT * FROM {$this->table} where 1=1";
		if(isset($conditions['promo'])){
			$sql .= ' and promo = \''.$conditions['promo'].'\'';
		}elseif(isset($conditions['pc'])){
			$sql .= ' and pc = \''.$conditions['pc'].'\'';
		}else{
			if(isset($conditions['promo']))
				$sql .= ' and promo = \''.$conditions['promo'].'\'';
			if(isset($conditions['yxqtime']))
				$sql .= ' and yxqtime >= \''.$conditions['yxqtime'].'\'';
			
			if(isset($conditions['mz']))
				$sql .= ' and mz like "%'.$conditions['mz'].'"';
		}
		$sql .= " order by id desc, id desc limit $start,$limit";
        return $this->getConnection()->fetchAll($sql) ? : null;
	}
	public function getMyPromo($userid)
	{
		
		if (is_numeric($userid)) {
			$sql = "SELECT * FROM {$this->table} where 1=1";
		}else{
			$sql = "SELECT * FROM {$this->table} where 1=1";
		}
		if(is_numeric($userid)){
			$sql .= ' and id = \''.$userid.'\'';
		}else{
			$sql .= ' and promo = \''.$userid.'\'';
		}	
        
		
        return $this->getConnection()->fetchAll($sql) ? : null;
	}
	
	public function getmz($promo)
	{
		$sql = "SELECT * FROM {$this->table} where promo='$promo' ";
		//var_dump($sql);exit;
        return $this->getConnection()->fetchAssoc($sql) ? : null;
	}

	public function buildding($conditions)
	{
		$sql = ' select promo('.$conditions['num'].','.$conditions['yxq'].', \''.$conditions['mz'].'\', \''.$conditions['pc'].'\');';
		return  $this->getConnection()->fetchColumn($sql);
	}

	public function getPromoCount($conditions)
	{
		$sql = " select count(id) from {$this->table} as num where 1=1";

		if(isset($conditions['promo'])){
			$sql .= ' and promo = \''.$conditions['promo'].'\'';
		}
		if(isset($conditions['pc'])){
			$sql .= ' and pc = \''.$conditions['pc'].'\'';
		}	
		
		return  $this->getConnection()->fetchColumn($sql);
	}

	public function set($conditions)
	{
		$sql = "update {$this->table} set validity = ".$conditions['validity']." where id in (0";
		foreach($conditions['ids'] as $id)
		{
			$sql .=",$id";
		}
		$sql .= ")";
		return  $this->getConnection()->exec($sql);
	}
	public function updatestatus($userid)
	{
		$sql = "update {$this->table} set status = 1 where Promo = '$userid'";
		return  $this->getConnection()->exec($sql);
	}
	public function reset($conditions)
	{
		$sql = "update {$this->table} set status=0,yxqtime=null,endtime=null where id in (0";
		foreach($conditions['ids'] as $id)
		{
			$sql .=",$id";
		}
		$sql .= ")";
		return  $this->getConnection()->exec($sql);
	}

	public function activing($conditions)
	{
		if(isset($conditions['Promo']))
			$sql = "select status,promo from {$this->table} where Promo = '{$conditions['promo']}' limit 1";
		else
			return array("status"=>"err","message"=>"�����뼤����", 'id'=>1);

		$res = $this->getConnection()->fetch($sql);
		if($res == null){
			return array("status"=>"err","message"=>'�ü����벻����', 'id'=>2);
		}elseif($res['status']){
			return array("status"=>"err","message"=>"�ü������ѱ�����", 'id'=>3);
		}

		$sql = "select promo,mz from activetry where machine = '{$conditions['machine']}' limit 1";
		$userinfo = $this->getConnection()->fetch($sql);
		$conditions = array_merge($conditions,$userinfo);

		$sql = "update {$this->table} set";

		if(isset($conditions['mz']))
			$sql .="mz = '".$conditions['mz']."',";
		$sql .= " status = 1, yxqtime='".strtotime("now")."',endtime='".strtotime("+".$res['validity']."month")."' ";
		$sql .= "where Promo='".$conditions['Promo']."'";
		$res = $this->getConnection()->exec($sql);
		if($res > 0){
			return array("status"=>"ok","message"=>"����ɹ�", 'id'=>0);
		}else{
			return array("status"=>"err","message"=>"����ʧ��", 'id'=>4);
		}
	}

	public function getstatus($Promo)
	{
		$sql = "select status from {$this->table} where Promo = '$Promo' limit 1";
		$res = $this->getConnection()->fetch($sql);
		if(($res['endtime']+0) < time())
		{
			$sql = "update {$this->table} set status = 0 where Promo = '$Promo'";
			$this->getConnection()->exec($sql);
			return array("status"=>"err","message"=>"�ü������ѵ���", 'id'=>1);
		}elseif(!$res['status'] && $res['endtime'])
		{
			return array("status"=>"err","message"=>"�ü������ѵ���", 'id'=>1);
		}elseif(!$res['status'])
		{
			return array("status"=>"err","message"=>"�ü�����δ����", 'id'=>2);
		}else
		{
			return array("status"=>"ok","message"=>"�ü�������ʹ����", 'id'=>0);
		}
	}

	public function gettrystatus($conditions)
	{
		$sql = "select id from activetry where machine = '{$conditions['machine']}' and mac = '{$conditions['mac']}' limit 1";
		$res = $this->getConnection()->fetchColumn($sql);
		if($res){
			return array("status"=>"err","message"=>"������", 'id'=>1);
		}else{
			return array("status"=>"ok","message"=>"δ����", 'id'=>0);
		}
	}

	public function trying($conditions){

//		$sql="select id from user where nickname = '{$conditions['xingming']}'";
//		$userid = $this->getConnection()->fetchColumn($sql);
//		if($userid){
//			return array("status"=>"err","message"=>"�ظ�����","id"=>2);
//		}

		$sql="select id from activetry where mac = '{$conditions['mac']}' and machine = '{$conditions['machine']}'";
		$id = $this->getConnection()->fetchColumn($sql);
		if(!$id){
			$sql  = "insert into activetry (machine,mac,yxqtime,xingming,mobile,qq,mz)";
			$sql .= "value('{$conditions['machine']}','{$conditions['mac']}','".time();
			$sql .= "','{$conditions['xingming']}','{$conditions['mobile']}','{$conditions['qq']}','{$conditions['mz']}')";
		}else{
			$sql  = "update activetry set xingming='{$conditions['xingming']}',mobile='{$conditions['mobile']}',yxqtime= '";
			$sql .= time()."',qq='{$conditions['qq']}',mz='{$conditions['mz']}'";
			$sql .= " where id = $id";
		}

		$success = $this->getConnection()->exec($sql);
		if($success)
			return array("status"=>"ok","message"=>"���óɹ�", 'id'=>0);
		else
			return array("status"=>"err","message"=>"����ʧ��", 'id'=>1);
	}
	
	public function getPromoByMachine($conditions){
		$sql="select Promo from {$this->table} where mac = '{$conditions['mac']}' and machine = '{$conditions['machine']}'";
		return $this->getConnection()->fetchColumn($sql);
	}

	public function gettrylist($conditions,$start,$limit){
		$sql = "SELECT * FROM activetry where 1=1";
		if(isset($conditions['machine'])){
			$sql .= ' and machine = \''.$conditions['machine'].'\'';
		}elseif(isset($conditions['mac'])){
			$sql .= ' and mac = \''.$conditions['mac'].'\'';
		}else{
			if(isset($conditions['yxqtime']))
				$sql .= ' and yxqtime >= \''.$conditions['yxqtime'].'\'';
			
			if(isset($conditions['mz']))
				$sql .= ' and mz like "%'.$conditions['mz'].'%"';
		}
		$sql .= " order by id desc limit $start,$limit";
        return $this->getConnection()->fetchAll($sql) ? : null;
	}

	public function gettryCount($conditions)
	{
		$sql = " select count(id) from activetry as num where 1=1";

		if(isset($conditions['yxqtime']))
			$sql .= ' and yxqtime > \''.$conditions['yxqtime'].'\'';
		
		if(isset($conditions['mz']))
			$sql .= ' and mz like "%'.$conditions['mz'].'%"';
		return  $this->getConnection()->fetchColumn($sql);
	}
}